/*
 * user : hxx
 * 
 * time : 2018-01-15
 * 
 * 此类将运行的对象添加入库，每个类保持一份，
 * 
 * tostring 会自动将该类中的变量，公共，静态，及有公共方法可见的变量，串化字符串返回；
 * 
 * 及时的查看，并且可以修改该类变量的值；
 * 
 * Web test
 */

package com.hxx.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hxx.lib.Lutra;


/**
 * Servlet implementation class look
 */
public class MyDeskWeb extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public static String getRemortIP(HttpServletRequest request) { 
	    String ip = request.getHeader("x-forwarded-for"); 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("Proxy-Client-IP"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("WL-Proxy-Client-IP"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("HTTP_CLIENT_IP"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getRemoteAddr(); 
	    } 
	    return ip; 
	}		

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyDeskWeb() {
        super();
        // TODO Auto-generated constructor stub
    }
    

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		response. setCharacterEncoding("UTF-8");	
		
		PrintWriter pw = response.getWriter();	
		String ip=getRemortIP(request);
		pw.println("welcome "+ip+" in  console<br>");
		
		
		
		String classpath=Thread.currentThread().getContextClassLoader().getResource("/").getPath();  //LibJni.class.getResource("/").getPath();		
		pw.println(classpath);
		
		classpath=Thread.currentThread().getContextClassLoader().getResource(".").getPath();		
		pw.println(classpath);
		
		classpath=MyDeskWeb.class.getResource("/").getPath();		
		pw.println(classpath);
		
		
		classpath=MyDeskWeb.class.getResource(".").getPath();		
		pw.println(classpath);
		
		
		String key = request.getParameter("classname");//
		if (key==null || key.isEmpty())
			key="all";
		
		
		SmsMsg so=new SmsMsg();		
		so.setMobile("18588888888");
		so.setContent("328888888888888888888888888888888888888888899999999");
		so.setSendtime("1245789966");	
		so.setSeqid("5548889999999");
		so.setSin("0000");
		so.a=8888;
		so.setB(123);
		
		Lutra.addOj(so);
		
		//key == "all"  or key ==  classname  
		ConcurrentHashMap<String, Object>  ret=Lutra.getOjMap(key);		
		for (Object value : ret.values()) {		  
			pw.println(Lutra.obToString(value));	  
		}		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
